<template>
  <view>
    <!-- 搜索组件 -->
    <my-search sticky @click="toSearch"></my-search>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" class="swiper" :circular="true">
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <image :src="item.image_src" mode="widthFix"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 菜单导航 -->
    <view class="menu-list">
      <view class="menu-list-item" v-for="(item, index) in navList" :key="index" @click="handleClickNav(item)">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>
    <!-- 楼层列表 -->
    <view class="floor">
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <view class="floor-title">
          <image :src="item.floor_title.image_src" mode="" class="floor-title-image"></image>
        </view>
        <view class="floor-content">
          <view class="floor-left">
            <navigator :url="item.product_list[0].url">
              <image :src="item.product_list[0].image_src" :style="{ width: item.product_list[0].image_width + 'rpx' }"
                mode="widthFix"></image>
            </navigator>
          </view>
          <view class="floor-right">
            <navigator :url="d2.url" v-for="(d2, index)  in item.product_list" :key="index">
              <image :src="d2.image_src" v-if="index !== 0" :style="{ width: d2.image_width + 'rpx' }" mode="widthFix">
              </image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import Badge from '../../mixins/tabbar-badge.js'
  export default {
    mixins: [Badge],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: [],
      };
    },
    methods: {
      toSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },
      getFloorList() {
        uni.request({
          url: 'https://www.uinav.com/api/public/v1/home/floordata',
          method: 'GET',
          success: ({
            data
          }) => {
            const {
              message,
              meta
            } = data;
            if (meta.status !== 200) {
              uni.showToast({
                title: '数据请求失败',
                icon: 'error',
                duration: 1500
              })
              return;
            }
            // 对原始数据进行二次处理
            message.forEach(v => {
              v.product_list.forEach(v => {
                v.url = `/subpkg/goods_list/goods_list?` + v.navigator_url.split('?')[1]
              })
            })

            this.floorList = message
          }
        })
      },
      handleClickNav(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/classify/classify'
          })
          return;
        }
      },
      getNavList() {
        uni.request({
          url: 'https://www.uinav.com/api/public/v1/home/catitems',
          method: 'GET',
          success: ({
            data
          }) => {
            const {
              message,
              meta
            } = data;
            if (meta.status !== 200) {
              uni.showToast({
                title: '数据请求失败',
                icon: 'error',
                duration: 1500
              })
              return;
            }
            this.navList = message
          }
        })
      },
      getSwiperList() {
        uni.request({
          url: 'https://www.uinav.com/api/public/v1/home/swiperdata',
          method: 'GET',
          success: ({
            data
          }) => {
            const {
              message,
              meta
            } = data;
            if (meta.status !== 200) {
              uni.showToast({
                title: '请求失败',
                icon: "error"
              })
              return;
            }
            this.swiperList = message
          }
        })
      }
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    }
  }
</script>

<style lang="scss">
  .swiper {
    image {
      width: 100%;
      height: 300rpx;
    }
  }

  .menu-list {
    display: flex;
    justify-content: space-around;
    margin: 16rpx 0;

    .menu-list-item {
      image {
        height: 150rpx;
        width: 128rpx;
      }
    }
  }

  .floor-title-image {
    height: 60rpx;
  }

  .floor {
    padding: 10rpx;

    .floor-content {
      display: flex;

      .floor-right {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }
    }
  }
</style>
